<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="规则名称" prop="engineName">
        <el-input
          v-model="queryParams.engineName"
          placeholder="请输入规则名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代码类型" prop="codeType">
        <el-select v-model="queryParams.codeType" placeholder="请选择代码类型">
          <el-option
            v-for="item in codeTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类/函数名称" prop="clName" label-width="120px">
        <el-input
          v-model="queryParams.clName"
          placeholder="请输入类/函数名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery" size="mini">搜索</el-button>
     
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
         <el-button type="success"
         v-waves="['system:engine:add']"
         
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
         >新增规则</el-button>
       
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="engineList" @selection-change="handleSelectionChange">
      <el-table-column label="规则名称" align="center" prop="engineName" />
      <el-table-column label="规则类型id" align="center" prop="engineTypeId" />
      <el-table-column label="代码类型" align="center" prop="codeType" />
      <el-table-column label="类/函数名称" align="center" prop="clName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="260px">
        <template slot-scope="scope">
          <el-button
            v-waves="['system:engine:edit']"
            size="mini"
            type="text"
           
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-waves="['system:engine:edit']"
            size="mini"
            type="text"
           
            @click="handleUpdate2(scope.row)"
          >编写代码</el-button>
          <el-button
          class="del"
            v-waves="['system:engine:remove']"
            size="mini"
            type="text"
            
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据规则引擎对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="规则名称" prop="engineName">
          <el-input v-model="form.engineName" placeholder="请输入规则名称" />
        </el-form-item>
        <el-form-item label="规则类型" prop="engineTypeId">
          <el-select v-model="form.engineTypeId" placeholder="请选择规则类型">
            <el-option
              v-for="item in engineTypeIdList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="代码类型" prop="codeType">
          <el-select v-model="form.codeType" placeholder="请选择代码类型">
            <el-option
              v-for="item in codeTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="类/函数名称" prop="clName">
          <el-input v-model="form.clName" placeholder="请输入类/函数名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 编写代码 -->
    <el-dialog title="编写代码" :visible.sync="openCode" width="1200px" append-to-body>
      <div class="editor-container">
        <json-editor v-if="openCode" ref="jsonEditor" v-model="codeRow.engineCode" :language="codeRow.codeType" :name="codeRow.className" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEngine, getEngine, delEngine, addEngine, updateEngine, listEngineType, updateCode } from '@/api/engine'
import waves from '@/directive/waves'
import JsonEditor from '@/components/JsonEditor'

export default {
  name: 'Index',
  components: { JsonEditor },
  directives: { waves },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据规则引擎表格数据
      engineList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openCode: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        engineName: null,
        codeType: null,
        clName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        engineName: [
          { required: true, message: '规则名称不能为空', trigger: 'blur' }
        ],
        engineTypeId: [
          { required: true, message: '规则类型不能为空', trigger: 'blur' }
        ],
        codeType: [
          { required: true, message: '代码类型不能为空', trigger: 'change' }
        ],
        clName: [
          { required: true, message: '类/函数名称不能为空', trigger: 'blur' }
        ]
      },
      // 代码类型
      codeTypeList: [{
        value: 'java',
        label: 'java'
      }, {
        value: 'sql',
        label: 'sql'
      }],
      // 规则类型
      engineTypeIdList: [],
      // 代码
      codeRow: {
        engineId: '',
        code: '',
        codeType: '',
        className: ''
      }
    }
  },
  created() {
    this.getList()
    this.getlistEngineType()
  },
  methods: {
    /** 查询数据规则引擎列表 */
    getList() {
      this.loading = true
      listEngine(this.queryParams).then(response => {
        this.engineList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 获取规规则类型
    getlistEngineType() {
      listEngineType().then(response => {
        console.log(response.data)
        response.data.forEach(element => {
          this.engineTypeIdList.push({
            value: element.id,
            label: element.engineType
          })
        })
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openCode = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        engineName: null,
        engineTypeId: null,
        codeType: null,
        clName: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加数据规则引擎'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getEngine(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改数据规则引擎'
      })
    },
    /** 编辑代码按钮操作 */
    handleUpdate2(row) {
      this.codeRow = row
      this.codeRow.engineId = row.id
      this.codeRow.codeType = row.codeType
      this.codeRow.className = 'ETL_RULE_' + Date.parse(new Date()) / 1000
      this.codeRow.engineCode = row.engineCode ? row.engineCode : ''
      console.log(this.codeRow)
      this.openCode = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEngine(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addEngine(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    // 代码编写提交
    submitForm2() {
      console.log(this.value, 'aaaaaa', this.codeRow)
      updateCode(this.codeRow).then((response) => {
        if (response.code === 200) {
          this.msgSuccess('编辑成功')
          this.openCode = false
          this.getList()
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除名称为"' + row.clName + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delEngine(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/engine/export', {
        ...this.queryParams
      }, `system_engine.xlsx`)
    }
  }
}
</script>

<style scoped>
.editor-container{
  position: relative;
  height: 100%;
}
.del{
  color: red;
}
</style>
